<?php
include_once 'protocol_includes.php';

class RequestVersionUpload extends subapplication
{
    private $cmd;
    public static $PROTOCOL = "RequestVersionUpload";

    public static function create(command $cmd)
    {
        if (!self::IsThisClass($cmd)) return null;
        $N = new RequestVersionUpload($cmd);
        return $N;
    }


    public function __construct(command $cmd)
    {
        $this->cmd  = $cmd;
        $this->LoginKey($cmd->GetData('LOGINKEY'));
        $this->SoftwareID($cmd->GetData('SOFTWAREID'));
        $this->VersionNumber($cmd->GetData('VERSIONNUMBER'));
    }

    public function  requiresAuthentication() {
        return true;
    }
    
    public function execute()
    {
        $returnString = "NS_PROTOCOL=RequestVersionUploadReply&";
        $comfirmationString = "NS_COMFIRMATION=false";
        $result = ns_dao_software::CreateSoftwareVersion($this->SoftwareID(), $this->VersionNumber());
        if ($result > 0) {
            $softwareVersion = ns_dao_software::SoftwareVersionLatestBySoftwareIDWithInActive($this->SoftwareID());
            if ($softwareVersion != null) {
                $filePath = "/content/software/" . $softwareVersion->SoftwareID() . "/" . $softwareVersion->SoftwareVersionID() . "/";
                $comfirmationString = "NS_COMFIRMATION=true&NS_FILEPATH=" . $filePath . "&NS_SOFTWAREVERSIONID=" . $softwareVersion->SoftwareVersionID();
            }
        }
        $returnString = $returnString . $comfirmationString;
        $this->UpdateResult($returnString);
    }


    // =========== PROPERTIES ==========

    private $property = array();
    private function GetSetData($property_name,$value = null)
    {
        if (is_null($value) && isset($this->property[$property_name])) return $this->property[$property_name];
        $this->property[$property_name] = $value;
    }

    public function LoginKey($value = null)
    {
        return $this->GetSetData("LOGINKEY",$value);
    }

    public function SoftwareID($value = null)
    {
        return $this->GetSetData("SOFTWAREID",$value);
    }

    public function VersionNumber($value = null)
    {
        return $this->GetSetData("VERSIONNUMBER",$value);
    }

    public function result()
    {
        $this->cmd->GetResult();
    }

    public function UpdateResult($str)
    {
        $this->cmd->SetResult($str);
    }

    public static function IsThisClass(command $cmd)
    {
        if (!$cmd->HasDataKey('LOGINKEY')) return false;
        if (!$cmd->HasDataKey('SOFTWAREID')) return false;
        if (!$cmd->HasDataKey('VERSIONNUMBER')) return false;

        return true;
    }
}

?>
